<?php
/**
 * Class model that provides the base functionality to manage Users data
 *
  * This class was defined following the indications of
 * {@link http://www.overzealous.com/dmz/index.html DMZ ORM}. So,
 * this manage inserts, updates and deletes sql operations, as well as fully
 * set of instructions that can be used to make queries. And much more
 *
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage model
 */
class User extends PwModel 
{
    public $table      = "users";
    public $model      = "user";
    public $has_many   = array("project");
    public $has_one    = array();
    public $validation = array(
        'username' => array(
            'label' => 'Username',
            'rules' => array('trim', 'strtolower', 'required', 'unique',
                             'alpha_dash_dot', 'min_length' => 5)
        ),
        'password' => array(
            'label' => 'Password',
            'rules' => array('required', 'min_length' => 6, 'md5')
        ),
        'lastname' => array(
            'label' => 'Lastname',
            'rules' => array('trim', 'required')
        ),
        'firstname' => array(
            'label' => 'First Name',
            'rules' => array('trim', 'required')
        ),
        'email' => array(
            'label' => 'Email',
            'rules' => array('trim', 'required', 'valid_email')
        ),
        'phone' => array(
            'label' => 'Phone Number',
            'rules' => array('trim')
        ),
        'address' => array(
            'label' => 'Postal Address',
            'rules' => array('trim')
        )
    );

    public function __construct($id = NULL) 
    {
        parent::__construct($id);
    }

    /**
    * Checks if user is logged by getting the cookie
    *
    * @return boolean
    */
    public static function logged() 
    {
        $logged = get_cookie('protoweb_logged');
        if ($logged) {
            return (new User($logged));
        }
        else {
            return (FALSE);
        }
    } // end of static method logged()

    /**
     * Checks if a user is member of a project
     *
     * @param integer $id project id
     * @return boolean True if user instance is member of the project with the id = $id
     */
    public function is_member_of($id) 
    {
        if ($this->project->where("id",$id)->get()->count() > 0) {
            return (TRUE);
        }
        else {
            return (FALSE);
        }
    }

    /**
     * This functions returns the fullname of a user.
     *
     * @return string Returns the concatenation of lastname and firstname
     */
    public function fullname() 
    {
        return ($this->lastname . ' ' . $this->firstname);
    }

} // Class User

/* End of file user.php */
/* Location: ./application/models/user.php */